{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div id="tip_modal">
    <form class="layui-form p-4">
        <h3 class="pb-3">功能菜单/节点</h3>
        <table class="layui-table layui-table-form">
            <tr>
                <td class="layui-td-gray">供应商
                </td>
                <td>
                    <select name="punter_id" id="punter_id" {if isset($detail.punter_id)&& $detail.punter_id} disabled {/if}lay-search lay-verify="" lay-filter="punter_id">
                        <option value="">请选择</option>
                        {volist name='punterList' id='pl'}
                        <option value="{$pl.id}" {if isset($detail.punter_id) &&$detail.punter_id==$pl.id} selected {/if}>{$pl.name}</option>
                        {/volist}
                    </select>
                </td>
                <td class="layui-td-gray">付款时间<font>*</font>
                </td>
                <td>
                    <input type="text" class="layui-input" value="{$detail.orderDate|default=$defalutTime}" readonly name="orderDate" id="orderDate" placeholder="选择时间"
                           required>
                </td>
            </tr>


            <tr>
                <td class="layui-td-gray">付款金额<font>*</font>
                </td>
                <td>
                <span id="paymentPrice">{$detail.paymentPrice|default=0}</span>
                </td>
                <td>付款人</td>
                <td>
                    <span>{$detail.username|default=''}</span>
                </td>

            </tr>
            <tr>
                <td class="layui-td-gray">状态<font>*</font>
            </td>
                <td>
                    <select name="status" disabled>
                        {foreach [['id'=>1,'name'=>'草稿'],['id'=>2,'name'=>'已确认']] as $st}
                        <option value="{$st.id}" {if isset($detail.status)&&$detail.status==$st.id } selected {/if}>{$st.name}</option>
                        {/foreach}
                    </select>

                </td>
            </tr>

            <tr>
                <td colspan="6"><h3>明细</h3></td>
            </tr>
            <tr>
                <td colspan="6">
                    {if !isset($detail.status)||$detail.status==1}
                    <a href="javascript:" onclick="XZMX()" class="layui-btn layui-btn-primary">选择明细</a>
                    <a href="javascript:" id="addMxbtn" style="display: none"  data-modal="" class="layui-btn layui-btn-primary">选择明细</a>
                    {/if}
                </td>
            </tr>
            <table class="layui-table tableMany">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>采购对账单</th>
                    <th>应付金额</th>
                    <th>未付款金额</th>
                    <th>本次付款金额</th>
                    <th>操作</th>
                </tr>
                </thead>
                {if isset($mxList)}
                <tbody id="mxTbdy">
                {volist name='mxList' id='ml' key='k'}
                <tr>
                    <td><span class="xuhao">{$k}</span>
                        <input type="hidden" name="mxid[]" value="{$ml.id}">
                        <input type="hidden" name="sale_reconcile_id[]" value="{$ml.sale_reconcile_id}">
                        <input type="hidden" name="oldNum[]" value="{$ml.unPrice}">
                    </td>

                    <td>{$ml.srOrderNum}</td>
                    <td>{$ml.reconcilePrice}</td>
                    <td>{$ml.unPrice}</td>
                    <td><input type="text" value="{$ml.money}" class="layui-input moneyInput" maxlength='9' required
                               lay-verify="required" onblur="countPrice()" onkeyup="digitalFiltering(this,0)" placeholder="请输入金额"
                               autocomplete="off" min="0" max="999999" name="money[]"></td>
                    <td>
                        {if !isset($detail.status)||$detail.status==1}
                        <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger"
                           onclick="delMx('{$ml.id}',this)">删除</a>
                        {/if}
                    </td>
                </tr>
                {/volist}
                </tbody>
                {else}
                <tbody id="mxTbdy">
                </tbody>
                {/if}

            </table>
        </table>
        <div class="py-3">
            <input type="hidden" name="id" value="{$id}">
            {if !isset($detail.status)||$detail.status==1}
            <button class="layui-btn layui-btn-normal" type="button" id="sub" lay-submit="" lay-filter="webform1">保存 </button>
           {/if}
            {if isset($detail.status)&&$detail.status==1}
            <button type="button" class="layui-btn layui-btn-normal" onclick="onSure()">确认</button>
            {/if}

        </div>
    </form>
</div>

{/block}
<!-- /主体 -->
{block name="script"}
<script>

    const moduleInit = ['tool'];

    let isSub = 2;
    var punter_id="{$detail.punter_id|default=''}";
    function gouguInit() {
        var form = layui.form, tool = layui.tool;
        //监听提交
        form.on('submit(webform1)', function (data) {
            let callback = function (e) {
                isSub = 2;
                layer.msg(e.msg);
                if (e.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            }
            if (isSub == 2) {
                isSub = 1
                tool.post("{:url('add')}", data.field, callback);
            }
            return false;
        });

        // layui.use(['form'], function () {
        //     var form = layui.form;
        //     form.render();
        // });
        form.on('select(punter_id)', function (data) {
            punter_id = data.value;

        }); // select(lay-filter属性的值)
        //动态获取联动
        //弹窗
        var treeTable = layui.table, form = layui.form;
        var tableIns = treeTable.render({
            elem: '#mxTable'
            , url: "{:url('addMx')}?punter_id="+punter_id
            , method: 'post'
            , toolbar: '#toolbarDemo',
            defaultToolbar: []
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left'},
                {fixed: 'left', field: 'id', title: 'ID', unresize: true, hide: true}
                // ,{field: 'id', width: 80, title: 'ID号', align: 'center'}
                , {field: 'orderNum', title: '单据编号'}
                , {field: 'reconcile_type', title: '业务类型',templet:function (d) {
                    if (d.reconcile_type==1){
                        return '产品对账';
                    }else {
                        return  '金额对账';
                    }
                    }}
                , {field: 'reconcilePrice', title: '应收金额'}
                , {field: 'unPrice', title: '未回款金额'}
            ]]
            , page: true
        });
        //监听搜索提交
        form.on('submit(webform)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //表头工具栏事件
        treeTable.on('toolbar(mxTable)', function (obj) {
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch (obj.event) {
                case 'add':
                    break;
                case 'subt':
                    var data = checkStatus.data;
                    var ids = []
                    if (data.length) {
                        data.forEach(function (value, index) {
                            ids.push(value)
                        })
                        saveAddMx(ids)
                        layer.close(layTip)

                    } else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;

            }
            ;
        })
        laydate.render({
            elem: '#orderDate'
            , type: 'date',
        });
    }
    function XZMX() {
        if (punter_id==''||!punter_id){
            layer.msg('请选择供应商');
            return;
        }
        $("#addMxbtn").attr('data-modal',"{:url('addMx')}?punter_id="+punter_id)
        $("#addMxbtn").click()
    }
</script>
<script>
    function saveAddMx(ojb) {
        var lastListItem = $('.tableMany tr:last');
        let aa = $(lastListItem).find(".xuhao")
        let len = $(aa).text()
        console.log('leng:', len)
        if (!parseInt(len)) {
            len = 0;
        }
        let index = parseInt(len) + 1;
        let str = '';
        for (var i = 0; i < ojb.length; i++) {
            str += "  <tr>\n" +
                "                <td> <span class=\"xuhao\">" + index + "</span> <input type=\"hidden\" name=\"mxid[]\" value=\"\">" +
                "<input type=\"hidden\" name=\"sale_reconcile_id[]\" value=\"" + ojb[i].id + "\">" +
                "</td>\n" +
                "                <td> " + ojb[i].orderNum + " </td>\n" +
                "                <td> " + ojb[i].reconcilePrice + " </td>\n" +
                "                <td>" + ojb[i].unPrice + " </td>\n" +
                "                <td>\n" +
                "                    <input type=\"text\" value='' class=\"layui-input moneyInput\" maxlength='9' onblur='countPrice()' required lay-verify=\"required\" onkeyup=\"digitalFiltering(this,2)\" placeholder=\"请输入付款金额\" autocomplete=\"off\" min=\"0\" max=\"999999\" name=\"money[]\">\n" +
                "                    <input type=\"hidden\" value='"+ojb[i].unPrice+"' class=\"layui-input \"  name=\"oldNum[]\">\n" +
                "                </td>\n" +
                "                <td>\n" +
                "                    <a href=\"javascript:\" class='layui-btn layui-btn-danger layui-btn-xs' onclick='removetr(this)'>删除</a>\n" +
                "                </td>\n" +
                "            </tr>";
            index++;
        }
        $("#mxTbdy").append(str)
        layui.use(['form'], function () {
            var form = layui.form;
            form.render();
        });
    }

    function removetr(e) {
        $(e).parent().parent().remove()
        countPrice();
    }

    function delMx(id = '', e) {
        if (id == '') {
            layer.msg('请选择数据')
            return;
        }
        $.ajax({
            url: "{:url('removeMx')}",
            type: 'post',
            dataType: 'json',
            data: {id: id},
            success: function (res) {
                if (res.code != 0) {
                    layer.msg(res.msg)
                } else {
                    removetr(e)
                }
            },
            error(e) {
                layer.msg('网络错误，请稍后')
            }
        })
    }

    let is_sure=2;
    function onSure() {
        let id="{$id}";
        if (id==0){
            layer.msg('操作有误');
            return;
        }
        if (is_sure==1){
            return;
        }
        is_sure=1;
        $.ajax({
            url:"{:url('onSure')}",
            type:'post',
            dataType:'json',
            data:{id:id},
            success:function (res) {
                is_sure=2;
                layer.msg(res.msg)
                if (res.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            },
            error(e){
                layer.msg('网络错误，请稍后重试')
                is_sure=2
            }
        })
    }

    function countPrice() {
        var mm=document.getElementsByClassName('moneyInput');
        var price=0;
        for (var i=0;i<mm.length;i++){
            var item=mm[i];
            var aa=$(item).val();
            if (aa!=''){
                price+=parseInt(aa)
            }
        }
        $("#paymentPrice").text(price);
    }
</script>
{/block}
<!-- /脚本 -->